package xyz.xtt.exchange.mq;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.exchange.service.IInnerService;
import xyz.xtt.exchange.service.ITblAssetAuditService;
import xyz.xtt.pay.api.enums.PayOrderBusinessTypeEnum;
import xyz.xtt.pay.api.enums.PayOrderStatusEnum;
import xyz.xtt.pay.client.response.TblPayOrderDto;

/**
 * @author dale
 * @date 2025/3/7
 **/
@Slf4j
@Component
@RocketMQMessageListener(topic = "${rocketmq.pay.order.status.change.topic:nft_pay_order_status_change}", selectorExpression = "PERSONAL_MINT_V2:PAY_CANCEL", consumerGroup = "${rocketmq.consumer.group.mint.order.cancel:nft_mint_pay_cancel_group}")
public class CancelAuditAfterPayCancelConsumer implements RocketMQListener<MessageExt> {
	@Autowired
	private IInnerService iInnerService;
	@Autowired
	private ITblAssetAuditService tblAssetAuditService;

	@Override
	public void onMessage(MessageExt msg) {
		String msgId = msg.getMsgId();
		int reConsumeTimes = msg.getReconsumeTimes();
		String message = new String(msg.getBody(), StandardCharsets.UTF_8);
		log.info("CancelAuditAfterPayCancelConsumer nft_pay_order_status_change msgId:{},reConsumeTimes:{},message: {}", msgId, reConsumeTimes, message);
		if (StringUtils.isEmpty(message)) {
			return;
		}
		if (!JSONUtil.isTypeJSON(message)) {
			log.warn("CancelAuditAfterPayCancelConsumer nft_pay_order_status_change Received message: {} is not json", message);
			return;
		}

		try {
			TblPayOrderDto payOrderDto = JSONUtil.toBean(message, TblPayOrderDto.class);
			if (Objects.isNull(payOrderDto) || Objects.isNull(payOrderDto.getPayOrderId())) {
				log.warn("CancelAuditAfterPayCancelConsumer nft_pay_order_status_change JSONUtil.toBean is null. message:{}", message);
				return;
			}
			// 检查payOrder是否存在
			TblPayOrderDto dbDataResult = iInnerService.fetchPayOrder(payOrderDto.getPayOrderId());
			if (Objects.isNull(dbDataResult)) {
				log.warn("CancelAuditAfterPayCancelConsumer can not find payOrder Received message: {} is not exist", message);
				return;
			}

			if (Objects.equals(payOrderDto.getBusinessType(), PayOrderBusinessTypeEnum.PERSONAL_MINT_V2.getCode())
					&& Objects.equals(payOrderDto.getPayStatus(), PayOrderStatusEnum.PAY_CANCEL.getCode())) {
				log
						.info("CancelAuditAfterPayCancelConsumer nft_pay_order_status_change start to cancel mint audit. payOrderId:{}", payOrderDto
								.getBusinessRefId());
				tblAssetAuditService.setAssetAuditStatus(payOrderDto.getAppUserId(), Long.valueOf(payOrderDto.getBusinessRefId()), 1);
			} else {
				log.warn("CancelAuditAfterPayCancelConsumer nft_pay_order_status_change Received message: {} is not PERSONAL_MINT_V2", message);
			}
		} catch (Exception e) {
			log.info("CancelAuditAfterPayCancelConsumer consume failed. message:{} ex:{}", message, e.getMessage(), e);
			throw new RuntimeException("CancelAuditAfterPayCancelConsumer Consumer Message exception", e);
		}
	}
}
